Page({

	data:{
		isShowRules:false
	},


	//跳转页面
	goPage(e){
		my.navigateTo({
			url: e.target.dataset.url
		});
	},

	login(){

		my.authorize({
			scopes: 'scope.userInfo',
			success: (res) => {
				my.getAuthUserInfo({
					success: (userInfo) => {

						let opa = {
							nick_name:userInfo.nickName,
							head_img:userInfo.avatar
						}

						getApp().cloud.function.invoke('client',opa,"login").then(res => {
							console.log(res); 
						})  
					}
				});
			},
		});

		
	},

	//开始游戏前判断剩余游戏次数是否充足
	startGame(){

		my.showLoading();

		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('获取剩余游戏次数=====>',res);
			
			const that = this;
			
      my.hideLoading({
        page: that,  // 防止执行时已经切换到其它页面，page指向不准确
			});

			if(res.data.game_left_num <= 0){
				my.navigateTo({
					url: '../drawResult/drawResult?end=gameNo'
				});
			}else{
				my.navigateTo({
					url: '../explain/explain'
				});
			}
		})
		
		
	},

	 


  onLoad(query) {
		
		this.login()

		if(query.nickName){
				my.authorize({
					scopes: 'scope.userInfo',
					success: () => {
						my.getAuthUserInfo({
							success: (userInfo) => {

								// my.alert({
								// 		content: JSON.stringify(query)
								// 	});

								if(userInfo.nickName == query.nickName){
									console.info('名字相同')

									return
								}

								let data = {
									avatta:query.avatar,
									nick_name:query.nickName,
									userId:query.openId
								}

								getApp().cloud.function.invoke('client',data,"share").then(res => {
									console.log('分享好友=====>',res); 
									// my.alert({
									// 	title: JSON.stringify(res),
									// 	content: JSON.stringify(query)
									// });
								})  


							}
						});
					},
				});
		}
		
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
